package com.youlu.campus.admin.qa;

import com.youlu.campus.admin.annotation.AuthParamTypeEnum;
import com.youlu.campus.admin.annotation.RequestActivityAuth;
import com.youlu.campus.admin.utils.AuthUtils;
import com.youlu.campus.admin.utils.ExcelUtils;
import com.youlu.campus.entity.ActivityQaInfo;
import com.youlu.campus.entity.ActivityQuestionInfo;
import com.youlu.campus.entity.VO.req.ActivityQaInfoQueryVO;
import com.youlu.campus.entity.VO.req.ActivityQaQuestionQueryVO;
import com.youlu.campus.entity.VO.req.QaQuestionImportVO;
import com.youlu.campus.service.qa.ActivityQaInfoService;
import com.yuelin.infrastructure.quantum.common.QResult;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
public class ActivityQaInfoController {
    @Autowired
    private ActivityQaInfoService activityQaInfoService;

    @PostMapping("/qa/create")
    @RequestActivityAuth(activityId = "#req.activityId")
    public QResult createQa(@RequestBody @NotNull ActivityQaInfo req) {
        boolean re = activityQaInfoService.create(req);
        if (re) {
            return QResult.success(re);
        }
        return QResult.fail("OK");
    }

    @PostMapping("/qa/update")
    @RequestActivityAuth(activityId = "#request.activityId")
    public QResult updateQa(@RequestBody ActivityQaInfo request) {
        boolean result = activityQaInfoService.update(request);
        return QResult.success(result);
    }

    @GetMapping("/qa/delete")
    @RequestActivityAuth(activityId = "#id",paramType = AuthParamTypeEnum.QA)
    public QResult deleteQa(@RequestParam("id") String id) {
        boolean result = activityQaInfoService.delete(AuthUtils.getComId(), id);
        return QResult.success(result);
    }

    @GetMapping("/qa/find-one")
    public QResult findOneQa(@RequestParam("id") String id) {
        ActivityQaInfo result = activityQaInfoService.findOne(id);
        return QResult.success(result);
    }

    @PostMapping("/qa/list")
    public QResult listQa(@RequestBody ActivityQaInfoQueryVO request) {
        return QResult.success(activityQaInfoService.list(request));
    }

    /**
     * 问题列表
     * @param req
     * @return
     */
    @PostMapping("/qa/question/list")
    public QResult listQuestion(@RequestBody ActivityQaQuestionQueryVO req) {
        return QResult.success(activityQaInfoService.listQuestion(req));
    }

    @PostMapping("/qa/question/create")
    public QResult createQuestion(@RequestBody ActivityQuestionInfo req) {
        return QResult.success(activityQaInfoService.createQuestion(req));
    }


    @PostMapping("/qa/question/import")
    public QResult qaQuestionImport(@RequestParam("file") MultipartFile file,
                                    @RequestParam String activityQaInfoId) {
        List<QaQuestionImportVO> excelDataList = ExcelUtils.readExcel(file, QaQuestionImportVO.class);
        if (CollectionUtils.isEmpty(excelDataList)) {
            return QResult.fail("导入文件错误");
        }
        activityQaInfoService.questionImport("admin",activityQaInfoId,excelDataList);
        return QResult.success(null);
    }


    @PostMapping("/qa/question/update")
    public QResult updateQuestion(@RequestBody ActivityQuestionInfo req) {
        return QResult.success(activityQaInfoService.updateQuestion(req));
    }

    @GetMapping("/qa/question/delete")
    public QResult deleteQuestion(@RequestParam("id") String id) {
        return QResult.success(activityQaInfoService.deleteQuestion("", id));
    }

    @GetMapping("/qa/question/find-one")
    public QResult findOneQuestion(@RequestParam("id") String id) {
        return QResult.success(activityQaInfoService.findOneQuestion("", id));
    }
}
